package com.tongji.housediy.view.renderer
{
	import com.tongji.housediy.events.commonEvent.ClearTitleWindowEvent;
	import com.tongji.housediy.events.commonEvent.HideTitleWinEvent;
	import com.tongji.housediy.events.commonEvent.ListItemMouseEvent;
	import com.tongji.housediy.model.GoodsModel;
	import com.tongji.housediy.utils.LoadAdmin;
	import com.tongji.housediy.utils.PopUpAdmin;
	import com.tongji.housediy.vo.Goods;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	
	import mx.controls.Alert;
	import mx.controls.ToolTip;
	import mx.core.IUIComponent;
	import mx.core.UIComponent;
	import mx.events.MoveEvent;
	import mx.managers.DragManager;
	import mx.managers.PopUpManager;
	import mx.managers.ToolTipManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.components.Group;
	
	/**
	 * 
	 * @author chenchao
	 * 
	 */
	public class GoodsItemRentererMediator extends Mediator
	{
		[Inject]
		public var view:GoodsItemRenterer;
		
		[Inject]
		public var model:GoodsModel;
		
		[Embed(source="assets/images/loading.swf")]
		private var EmbedLoading:Class;
		private var loadingMC:MovieClip;
		
		private var loader:Loader;

		public var existWindow:Boolean = false;
		public static var confirmRightTitleWindow:Boolean = true;
		
		override public function onRegister():void
		{
			view.enabled = false;
			
			loadingMC = new EmbedLoading();
			loadingMC.x = 15;
			loadingMC.y = 5;
			view.image.addChild(loadingMC);
			
			view.addEventListener(MouseEvent.MOUSE_DOWN, itemMouseDownHandler);
			view.addEventListener(MouseEvent.CLICK, itemClickHandler);

			view.image.addEventListener(Event.COMPLETE, loadingCompleteHandler);

			this.addContextListener(ClearTitleWindowEvent.SET_EXIST_WINDOW_FALSE, setExistWindowFalse, ClearTitleWindowEvent);
		}
		
		private function itemMouseDownHandler(event:MouseEvent):void
		{
			view.addEventListener(MouseEvent.MOUSE_MOVE, itemMouseMoveHandler);
		}
		
		private function itemClickHandler(event:MouseEvent = null):void
		{
			view.dispatchEvent(new ListItemMouseEvent(ListItemMouseEvent.ITEM_MOUSE_UP));			
		}
		
		private function setExistWindowFalse(event:ClearTitleWindowEvent):void
		{
			this.existWindow = false;
			event.stopPropagation();
		}
		
		private function itemMouseMoveHandler(event:MouseEvent):void
		{
			switch (view.data.tag)
			{
				case "door":
					view.dispatchEvent(new ListItemMouseEvent(ListItemMouseEvent.ITEM_MOUSE_DOWN, "doorFormat", event,
						(loader.content as Bitmap).bitmapData.clone()));
					break;
				case "window":
					view.dispatchEvent(new ListItemMouseEvent(ListItemMouseEvent.ITEM_MOUSE_DOWN, "windowFormat", event,
						(loader.content as Bitmap).bitmapData.clone()));
					break;
				case 3:
				case "floor":
					view.dispatchEvent(new ListItemMouseEvent(ListItemMouseEvent.ITEM_MOUSE_DOWN, "mapFormat", event,
						(loader.content as Bitmap).bitmapData.clone()));
					break;
				case "furniture":
					view.dispatchEvent(new ListItemMouseEvent(ListItemMouseEvent.ITEM_MOUSE_DOWN, "furnitureFormat", event,
						(loader.content as Bitmap).bitmapData.clone()));
					break;
			}
			view.removeEventListener(MouseEvent.MOUSE_MOVE, itemMouseMoveHandler);
		}		
		
		public function hideLoading():void
		{
			if (view.image.contains(loadingMC))
			{
				view.image.removeChild(loadingMC);
				view.enabled = true;
			}
		}
		
		private function loadingCompleteHandler(event:Event):void
		{
			if ((view.data as Goods).dragPicSrc != null)
			{
				loader = LoadAdmin.loadBitmapData((view.data as Goods).dragPicSrc);
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompleteHandler);
				hideLoading();
			}
			else
				hideLoading();
			//view.image.removeEventListener(Event.COMPLETE, loadingCompleteHandler);
		}
		
		private function loadCompleteHandler(event:Event):void
		{
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadCompleteHandler);
			hideLoading();
		}
	}
}